<?php

namespace App\Admin\Extensions;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Controllers\UserController;
use App\Admin\Controllers\UserBingshiController;
use App\Admin\Controllers\UserDanganController;
use App\Admin\Controllers\UserWenjuanController;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Tab;

class AddDangan {
    public function __construct(){

    }
    public function render()
    {
        $modal = Modal::make()
        ->title('复诊档案')
        ->body($this->view())
        ->xl()
        ->onShow(
    <<<JS
    $('.step-4').click(function(){
        $(this).parents('.modal-body').find('.nav-tabs li a:eq(1)').tab('show');
        window.location.hash = 'tab_wenjuan-form-tab';
   })
   $('.step-3').click(function(){
        $(this).parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
        window.location.hash = 'tab_dangan-form-tab';
   })

   $('.savedangan').click(function(){
        var dangan = {};
        var dangan = $('#tab_dangan-form-tab form ').serializeObject();

        console.log(dangan);
        if(dangan.user_id == '' || dangan.height == '' || dangan.weight == '') {
            $('.step-3').parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
            window.location.hash = 'tab_dangan-form-tab';
            $('#tab_dangan-form-tab form ').submit()
            Dcat.swal.error('错误', '档案信息请填写完整');
            return false
        }
        var wenjuan = $('#tab_wenjuan-form-tab form ').serializeObject();
        
        if(wenjuan.zhenduan == '') {
            $('.step-1').parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
            window.location.hash = 'tab_wenjuan-form-tab';
            $('#tab_wenjuan-form-tab form ').submit()
            Dcat.swal.error('错误', '问卷信息请填写完整');
            return false
        }
        console.log(dangan)
        ajaxUrl('/api/saveDang', {dangan, wenjuan}, function(res){
            Dcat.swal.success('提示', res.message).then((res)=>{
                Dcat.reload();
            })
            // $.admin.reload('刷新成功 !');
            return
        })
        
   })

JS
)
        ->button('<button class="btn btn-primary">复诊</button>');
        return str_replace('modal-xl', 'modal-1000', $modal->render());
    }


    public function danganForm(){
        Admin::translation('user-dangan');
        return app()->make(UserDanganController::class)->form(-1);
    }
    public function wenjuanForm(){
        Admin::translation('user');
        return app()->make(UserWenjuanController::class)->form(-1);
    }
    public function nextStepHtml(){
        return '<div class="row" style="padding-bottom:50px;"><div class="col-lg-12 text-center"><button class="btn btn-primary step-3">上一步</button> <button class="btn btn-primary mx-1 step-4">下一步</button><button type="submit" class="btn btn-primary savedangan">保存</button></div></div>';
    }

    public function view(){
        $tab = Tab::make();

        // 第一个参数是选项卡标题，第二个参数是内容，第三个参数是是否选中
        $tab->add('档案信息', $this->danganForm(), true, 'dangan-form-tab');
        $tab->add('问卷信息', $this->wenjuanForm(), false, 'wenjuan-form-tab');

        return $tab->withCard() . $this->nextStepHtml();
    }
}